from django.contrib.auth import authenticate, login, logout
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
from django.conf import settings
from flexy.sysmodel.models import *
from flexy.objects.models import *
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
import time

def login_view(request):
    if request.method == 'GET':
        c = {}
        c.update(csrf(request))
        return render_to_response('objects/login.html', c)
    else :
        username = request.POST['USERNAME']
        password = request.POST['PASSWORD']
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return HttpResponseRedirect('/objects/home/')
        return HttpResponseRedirect('/objects/login/')
    
def logout_view(request):
    logout(request)
    return HttpResponseRedirect('/objects/login/')
    
def get_user_group(request) :
    user = User.objects.get(id=request.user.id)
    usergroups = user.groups.all()
    for usergroup in usergroups :
        return usergroup
    return None
    
@login_required
def home(request):
    if request.method == 'GET':
        MEDIA_URL = settings.MEDIA_URL
        usergroup = get_user_group(request)
        usergroup_flexyclass = UserGroup_FlexyClass \
            .objects.filter(usergroup = usergroup, perm__gt = 0)
        return render_to_response('objects/home.html', locals())
    
@login_required
def add(request, class_id):
    if request.method == 'GET':
        c = {}
        c.update(csrf(request))
        flexyclass = FlexyClass.objects.get(id=class_id)
        c.update({'flexyclass' : flexyclass})
        usergroup = get_user_group(request)
        usergroup_field = UserGroup_Field \
            .objects.filter(usergroup = usergroup, perm__gt = 0,
                field__flexyclass__id = flexyclass.id) \
            .order_by('field__group__seq', 'field__group__id',
                'field__seq', 'field__id')
        c.update({'usergroup_field' : usergroup_field})
        return render_to_response('objects/add.html', c)
    else : 
        ret = Object.objects.add(request.POST, class_id)
        return render_to_response('common/ajax_response.json', ret)
    
@login_required
def update(request, class_id):
    if request.method == 'GET':
        return render_to_response('objects/update.html', locals())
    
@login_required
def bulkadd(request, class_id):
    if request.method == 'GET':
        return render_to_response('objects/bulkadd.html', locals())
    
@login_required
def bulkupdate(request, class_id):
    if request.method == 'GET':
        return render_to_response('objects/bulkupdate.html', locals())
    
@login_required
def search(request, class_id):
    if request.method == 'GET':
        return render_to_response('objects/search.html', locals())
    
@login_required
def get_nto1_list(request, field_id):
    if request.method == 'GET':
        print field_id
        return render_to_response('objects/nto1.json', locals())